package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.SetmealDto;
import com.itheima.mapper.CategoryMapper;
import com.itheima.mapper.SetmealDishMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.Category;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.SetmealDish;
import com.itheima.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    public R setmeal(SetmealDto setmealDto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDto, setmeal);
        setmealMapper.insert(setmeal);
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealId);
            setmealDishMapper.insert(setmealDish);
        }
        return R.success("添加成功");
    }

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public R findPage(Integer page, Integer pageSize, String name) {
        IPage<Setmeal> p = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        if (name != null) {
            wrapper.like(Setmeal::getName, name);
        }
        IPage<Setmeal> setmealIPage = setmealMapper.selectPage(p, wrapper);
        List<Setmeal> setmeals = setmealIPage.getRecords();

        String setmealsJsonString = JSON.toJSONString(setmeals);
        List<SetmealDto> setmealDtos = JSON.parseArray(setmealsJsonString, SetmealDto.class);
        for (SetmealDto setmealDto : setmealDtos) {
            Category category = categoryMapper.selectById(setmealDto.getCategoryId());
            String categoryName = category.getName();
            setmealDto.setCategoryName(categoryName);
        }
        String jsonString = JSON.toJSONString(setmealIPage);
        Page setmealDtoPage = JSON.parseObject(jsonString, Page.class);
        setmealDtoPage.setRecords(setmealDtos);
        return R.success(setmealDtoPage);
    }

    @Override
    public R selectBysetmealId(Long id) {
        Setmeal setmeal = setmealMapper.selectById(id);
        Long categoryId = setmeal.getCategoryId();
        Category category = categoryMapper.selectById(categoryId);

        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);

        setmealDto.setCategoryName(category.getName());
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(wrapper);
        setmealDto.setSetmealDishes(setmealDishes);
        return R.success(setmealDto);
    }

    @Override
    public R update(SetmealDto setmealDto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDto, setmeal);
        setmealMapper.updateById(setmeal);
        //  删掉原来的dish
        setmealDishMapper.deleteById(setmeal.getId());
        //  添加新的dish
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
            setmealDishMapper.insert(setmealDish);
        }
        return R.success("修改成功");
    }

    @Override
    public R status(Long[] ids, Integer id) {
        Setmeal setmeal = new Setmeal();
        LambdaUpdateWrapper<Setmeal> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(Setmeal::getStatus, id).in(Setmeal::getId, ids);
        setmealMapper.update(setmeal, wrapper);
        return R.success("修改成功");
    }

    @Override
    @Transactional
    public R deleteById(Long ids) {
        // 删除套餐   这个ids setmeal 的id
        setmealMapper.deleteById(ids);
        // 删除套餐的同时删除套餐内的菜品
        LambdaUpdateWrapper<SetmealDish> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, ids);
        setmealDishMapper.delete(wrapper);
        return R.success("删除成功");
    }

    @Override
    public R list(Long categoryId, Integer status) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId, categoryId);
        wrapper.eq(Setmeal::getStatus, status);
        List<Setmeal> setmeals = setmealMapper.selectList(wrapper);
        return R.success(setmeals);
    }

}
